package com.zmn.oms.admin.controller.conf.workturn.servcategory;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.business.interfaces.conf.workturn.servcategory.PlatServCategoryBService;
import com.zmn.oms.services.interfaces.conf.workturn.servcategory.PlatServCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/conf/workturn/servcategory/")
public class PlatServCategoryModifyController extends PlatServCategoryBaseController {

    @Autowired
    private PlatServCategoryService platServCategoryService;

    @Autowired
    private PlatServCategoryBService platServCategoryBService;

    /**
     * 保存服务分类
     *
     * @param plat
     *            平台
     * @param categoryId
     *            the category id
     * @return the result dto
     * @author linfeng
     * @since 2020/04/13
     */
    @RequestMapping("saveServCategory")
    @ResponseBody
    public ResponseDTO saveServCategory(Integer plat, Integer servCategId, String categoryId) {
        try {
            if (plat == null) {
                return ResponseDTO.fail("平台不能为空");
            }

            if (servCategId == null) {
                return ResponseDTO.fail("服务分类ID不能为空");
            }

            List<String> ids = StringUtil.isBlank(categoryId) ? Collections.EMPTY_LIST : Arrays.asList(categoryId.split(","));
            platServCategoryBService.modifyPlatServCategory(plat, servCategId, ids, getMcStaff());

            return ResponseDTO.success(plat, "保存开通分类成功");
        }catch (Exception e) {
            log.error("error:", e);
            return ResponseDTO.fail("保存开通分类失败");
        }
    }

    @RequestMapping("judgeDeleteCategory")
    @ResponseBody
    public ResponseDTO judgeDeleteCategory(Integer plat, Integer servCategId, String categoryId) {
        List<String> categoryIds = StringUtil.isBlank(categoryId) ? Collections.EMPTY_LIST : Arrays.asList(categoryId.split(","));
        List<String> dbCategorys = platServCategoryService.listByPlatAndServCategId(plat, servCategId).stream()
            .map(item -> item.getServCategId() + "-" + item.getCategOneId() + "-" + item.getCategId()).collect(Collectors.toList());

        // 过滤平台关闭的分类
        List<String> colseCategoryIds = dbCategorys.stream().filter(item -> !categoryIds.contains(item)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(colseCategoryIds)) {
            return ResponseDTO.success(true, GlobalConsts.SUCCESS_MSG);
        }

        return ResponseDTO.success(false, GlobalConsts.SUCCESS_MSG);
    }
}
